package com.quanyan.place.service.impl;

import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apiresp.RespAdminUserAuthInfo;
import com.quanyan.place.entity.apiresp.RespAdminUserInfo;
import com.quanyan.place.mapper.TbAdminAuthUserManagerMapper;
import com.quanyan.place.mapper.TbAdminUserMapper;
import com.quanyan.place.mapper.TbBizPlaceAuthUserMapper;
import com.quanyan.place.mapper.TbBizPlaceMapper;
import com.quanyan.place.service.PlaceAuthUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/14.
 * 场馆用户授权模块
 */
@Service
public class PlaceAuthUserServiceImpl implements PlaceAuthUserService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    TbAdminAuthUserManagerMapper tbAdminAuthUserManagerMapper;

    @Autowired
    TbAdminUserMapper tbAdminUserMapper;

    @Autowired
    TbBizPlaceAuthUserMapper tbBizPlaceAuthUserMapper;

    @Autowired
    TbBizPlaceMapper tbBizPlaceMapper;
    /**
     * 根据城市查询用户
     * @param reqAdminUserVo
     * @return
     */
    @Override
    public PageObj<List<RespAdminUserInfo>> getAdminUserByCity(ReqAdminUserVo reqAdminUserVo) {
        List<Integer> groupIdList = reqAdminUserVo.getGroupIdList();
        Integer pageNum = reqAdminUserVo.getPageNum();
        if (pageNum == null) {
            pageNum = 0;
        }
        Integer pageSize = reqAdminUserVo.getPageSize();
        if (pageSize == null) {
            pageSize = Constants.DEFAULT_PAGE_SIZE;
        }
        if (null == groupIdList){
            groupIdList = new ArrayList<>();
            groupIdList.add(21);
            groupIdList.add(22);
        }

        int total = tbAdminAuthUserManagerMapper.countByCondition(reqAdminUserVo);
        reqAdminUserVo.setPage(new Page(pageNum * pageSize, pageSize, total));

        List<TbAdminUser> adminUserList = tbAdminAuthUserManagerMapper.selectByCondition(reqAdminUserVo);
        List<RespAdminUserInfo> respAdminUserList = new ArrayList<>();
        for(TbAdminUser tbAdminUser:adminUserList){
            RespAdminUserInfo respAdminUserInfo = new RespAdminUserInfo();
            respAdminUserInfo.setMobile(tbAdminUser.getMobile());
            respAdminUserInfo.setTrueName(tbAdminUser.getTrueName());
            respAdminUserInfo.setId(tbAdminUser.getId());
            respAdminUserList.add(respAdminUserInfo);
        }
        PageObj<List<RespAdminUserInfo>> result = PageObj.create(total, pageNum, pageSize, respAdminUserList);
        return result;
    }
    /**
     * 插入用户及场馆对应关系
     * @param reqAdminUserVo
     * @return
     */
    @Override
    public Integer insertAuthUser(ReqAdminUserVo reqAdminUserVo) {
        TbBizPlaceAuthUserExample tbBizPlaceAuthUserExample = new TbBizPlaceAuthUserExample();
        TbBizPlaceAuthUserExample.Criteria tbBizPlaceAuthUserExampleCriteria = tbBizPlaceAuthUserExample.createCriteria();
        Integer placeId = reqAdminUserVo.getPlaceId();
        if (null != placeId){
            tbBizPlaceAuthUserExampleCriteria.andPlaceIdEqualTo(placeId);
        }
        Integer userId = reqAdminUserVo.getUserId();
        if (null != userId){
            tbBizPlaceAuthUserExampleCriteria.andUserIdEqualTo(userId);
        }
        Integer result = 0;
        try {
            List<TbBizPlaceAuthUser> tbBizPlaceAuthUserList = tbBizPlaceAuthUserMapper.selectByExample(tbBizPlaceAuthUserExample);
            //判断是否存在记录
            if (tbBizPlaceAuthUserList.size()==0){
                TbBizPlaceAuthUser tbBizPlaceAuthUser = new TbBizPlaceAuthUser();
                tbBizPlaceAuthUser.setPlaceId(placeId);
                tbBizPlaceAuthUser.setUserId(userId);
                Integer uId = reqAdminUserVo.getUid();
                tbBizPlaceAuthUser.setUid(uId == null ? null:uId);
                result = tbBizPlaceAuthUserMapper.insert(tbBizPlaceAuthUser);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 根据场馆ID查询所授权的用户
     * @param placeID
     * @return
     */
    @Override
    public RespAdminUserAuthInfo getAdminUserAuthByPlaceId(Integer placeID) {
        TbBizPlaceAuthUserExample tbBizPlaceAuthUserExample = new TbBizPlaceAuthUserExample();
        TbBizPlaceAuthUserExample.Criteria tbBizPlaceAuthUserExampleCriteria = tbBizPlaceAuthUserExample.createCriteria();
        tbBizPlaceAuthUserExampleCriteria.andPlaceIdEqualTo(placeID);
        RespAdminUserAuthInfo respAdminUserAuthInfo = null;
        try {
            List<TbBizPlaceAuthUser> tbBizPlaceAuthUserList = tbBizPlaceAuthUserMapper.selectByExample(tbBizPlaceAuthUserExample);
            List<TbAdminUser> tbBizPlaceAuthUsers = new ArrayList<>();
            for (TbBizPlaceAuthUser tbBizPlaceAuthUser:tbBizPlaceAuthUserList){
                Integer userId = tbBizPlaceAuthUser.getUserId();
                TbAdminUser tbAdminUser = new TbAdminUser();
                tbAdminUser.setId(userId);
                tbBizPlaceAuthUsers.add(getAdminUserByUserId(tbAdminUser));
            }
            respAdminUserAuthInfo = new RespAdminUserAuthInfo();
            respAdminUserAuthInfo.setPlaceId(placeID);
            respAdminUserAuthInfo.setList(tbBizPlaceAuthUsers);
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return respAdminUserAuthInfo;
    }


    /**
     * 查询用户详情
     * @param tbAdminUser
     * @return
     */
    public TbAdminUser getAdminUserByUserId(TbAdminUser tbAdminUser) {
        TbAdminUserExample tbAdminUserExample = new TbAdminUserExample();
        TbAdminUserExample.Criteria tbAdminUserExampleCriteria = tbAdminUserExample.createCriteria();
        Integer userId = tbAdminUser.getId();
        if (null != userId) {
            tbAdminUserExampleCriteria.andIdEqualTo(userId);
        }
        try {
            TbAdminUser tbAdminUser1 = tbAdminUserMapper.selectByExample(tbAdminUserExample).get(0);
            tbAdminUser.setId(tbAdminUser1.getId());
            tbAdminUser.setMobile(tbAdminUser1.getMobile());
            tbAdminUser.setTrueName(tbAdminUser1.getTrueName());
            tbAdminUser.setGender(tbAdminUser1.getGender());
            tbAdminUser.setLoginName(tbAdminUser1.getLoginName());
        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }

        return tbAdminUser;
    }


    /**
     * 删除（取消授权）
     * @param reqAdminUserVo
     * @return
     */
    @Override
    public Integer deleteAdminUserAuthByPlaceIdAndUserId(ReqAdminUserVo reqAdminUserVo) {
        TbBizPlaceAuthUserExample tbBizPlaceAuthUserExample = new TbBizPlaceAuthUserExample();
        TbBizPlaceAuthUserExample.Criteria tbBizPlaceAuthUserExampleCriteria = tbBizPlaceAuthUserExample.createCriteria();
        Integer placeId = reqAdminUserVo.getPlaceId();
        Integer userId = reqAdminUserVo.getUserId();
        if ( null != placeId && null != userId ){
            tbBizPlaceAuthUserExampleCriteria.andPlaceIdEqualTo(placeId);
            tbBizPlaceAuthUserExampleCriteria.andUserIdEqualTo(userId);
            return tbBizPlaceAuthUserMapper.deleteByExample(tbBizPlaceAuthUserExample);
        }
        return null;
    }


}
